<?php
//This in the main-controller for 'logged_in_users'.

session_start();
/**
 * Description of socialbuddy
 *
 * @author bilde
 */
class socialbuddy extends CI_Controller
{

	function  __construct()
        {
           parent::__construct();

           //Logged_in control
           if (!$this->session->userdata('is_logged_in'))
           {
                redirect('/welcome/index', 'refresh');
	   }
        }

        //Creates the home-page for the 'logged_in_user', which only he/she can see.
        //If any friend-request exist, it will be listed.
        //All your updates, and updates from people you follow, will be showen here, similar to the Facebook-newsfeed.
        function index()
        {
            $data['title'] = "Home";
            $data['main_content'] = 'socialbuddy/home';
            $data['user'] = $this->model_users->getUserByUsername($this->session->userdata('id'));
            $data['friend_requests'] = $this->model_friends->waiting_requests($this->session->userdata('id'));
            $following = $this->model_follows->get_following($this->session->userdata('id'));
            $following[] = $this->session->userdata('id');
            $data['updates'] = $this->model_updates->list_updates($following);
            //$data['comments'] = $this->model_comments->list_comments(array_keys($data['updates']),'updates');
            $data['usernames'] = $this->model_users->getAllUserNames();
            $data['following'] = $following;
            $this->load->vars($data);
            $this->load->view('includes/template');
     }

      //Loading the edit_profile view and passin user-infos as parameters.
      function edit_my_profile()
      {
        if (!$this->session->userdata('is_logged_in'))
        {
            redirect('/welcome/index', 'refresh');
        }
        else
        {
            $data['title'] = 'Edit Profile';
            $data['main_content'] = 'privateviews/edit_my_profile';
            $data['id'] = $this->session->userdata('id');
            $data['username'] = $this->session->userdata('username');
            $data['firstname'] = $this->session->userdata('firstname');
            $data['lastname'] = $this->session->userdata('lastname');
            $data['email'] = $this->session->userdata('email');
            $data['phone'] = $this->session->userdata('phone');
            $data['bio'] = $this->session->userdata('bio');
        }
        $this->load->vars($data);
	$this->load->view('includes/template');
     }

     //Saves the users changed infos to the database.
     function update_profile()
     {
        $id = $this->session->userdata('id');
        $try = $this->model_users->update_user($id);

        redirect("socialbuddy/index", 'refresh');
     }

     //Serac users, by term that could be anything in a given user-row.
     function search()
     {
        $input = $this->input->post('searchterm');
        $users_from_tags = $this->model_tags->search_tags($input);
        $data['results'] = $this->model_users->search_users($input,$users_from_tags);
        $data['title'] = 'Search Results';
        $data['main_content'] = 'socialbuddy/search_results';
        $data['user'] = $this->model_users->getUserByUsername($this->session->userdata('id'));
        $this->load->vars($data);
        $this->load->view('includes/template');
     }
}
?>
